package com.svmc.app.task.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import com.svmc.app.task.activity.R;
import com.svmc.app.task.tabmanager.TabGroupActivity;
import com.svmc.app.task.utils.CommonDefine;

public class DateDialog extends Activity implements OnClickListener,CommonDefine {
	DatePicker datePicker;
	TimePicker timePicker;
	int year,month,day,hour,minute;
	int year_re,month_re,day_re,hour_re,minute_re;
	int edit_or_new;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.datelayout_320);
		
		findViewById(R.id.datedialog_btn_set).setOnClickListener(this);
		findViewById(R.id.datedialog_btn_cancel).setOnClickListener(this);
		
		datePicker =(DatePicker)findViewById(R.id.datePicker1);
		timePicker =(TimePicker)findViewById(R.id.timePicker1);
		timePicker.setIs24HourView(true);
		/*
		 *  data to dialog time finish
		 */
		Bundle bundle_finish = getIntent().getExtras();
		edit_or_new =bundle_finish.getInt(EDIT_OR_NEW);
		Log.i("edit_or_new",""+ edit_or_new);
		year = bundle_finish.getInt("send_year");
		month =bundle_finish.getInt("send_month");
		//Log.i("month datedialog", ""+month);
		day=bundle_finish.getInt("send_day");
		hour =bundle_finish.getInt("send_hour");
		minute = bundle_finish.getInt("send_minute");
		datePicker.updateDate(year, month, day);
		timePicker.setCurrentHour(hour);
		timePicker.setCurrentMinute(minute);
		/*
		 * data to dialog time reminder
		 */
		/*
		Bundle bundle_reminder = getIntent().getExtras();
		year_re = bundle_reminder.getInt("send_year_reminder");
		month_re =bundle_reminder.getInt("send_month_reminder");
		day_re=bundle_reminder.getInt("send_day_reminder");
		hour_re =bundle_reminder.getInt("send_hour_reminder");
		minute_re = bundle_reminder.getInt("send_minute_reminder");
		datePicker.updateDate(year_re, month_re, day_re);
		timePicker.setCurrentHour(hour_re);
		timePicker.setCurrentMinute(minute_re);
		*/
	}
	@Override
	protected void onResume() {
		super.onResume();
		
	}

	@Override
	public void onClick(View v) {
		Intent timeIntent = new Intent();
		switch (v.getId()) {
		case R.id.datedialog_btn_cancel:			
			finish();
			break;
		case R.id.datedialog_btn_set:
			getDateAndTime();
			timeIntent.putExtra("year", year);
			timeIntent.putExtra("month", month);
			timeIntent.putExtra("day", day);
			timeIntent.putExtra("hour", hour);
			timeIntent.putExtra("minute", minute);
			timeIntent.putExtra(EDIT_OR_NEW, edit_or_new);
			//setResult(RESULT_OK,timeIntent);
			//finish();
			if (getParent() == null) {
			    setResult(Activity.RESULT_OK, timeIntent);
			} else {
			    getParent().setResult(Activity.RESULT_OK, timeIntent);
			}
			finish();
			break;
		default:
			break;
		}
		
	}
	private void getDateAndTime(){
		year = datePicker.getYear();
		month =datePicker.getMonth();
		day = datePicker.getDayOfMonth();
		hour =timePicker.getCurrentHour();
		minute=timePicker.getCurrentMinute();
	}
	public void showToast(String text){
		Toast.makeText(this, text, 0).show();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		TabGroupActivity.isdestroy=false;
		TabGroupActivity.isstop=false;
		datePicker=null;
		timePicker=null;
		System.gc();
	}

}
